/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.inmp.util;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.log4j.Logger;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-11-16 下午5:43:46          wangyazhou        1.0         To create
 * </p>
 *
 * @since     
 * @see     
 */
public class DBConnectionUtil {
    private static final Logger logger = Logger.getLogger(DBConnectionUtil.class);

    private static DBConnectionUtil instance = null;

    private BasicDataSource leDs = null;
    private BasicDataSource managerDs = null;
    private BasicDataSource storDs = null;
    private BasicDataSource gabDs = null;
    private BasicDataSource anjianDs = null;

    private DBConnectionUtil() {

    }

    /**
     * 保证DB2连接池只有一个
     * @return
     */
    public static synchronized DBConnectionUtil getInstance() {
        if (instance == null) {
            instance = new DBConnectionUtil();
        }
        return instance;
    }

    public void initLeDBPool(String driver, String url, String user, String pwd) {
        leDs = new BasicDataSource();
        leDs.setDriverClassName(driver);
        leDs.setUsername(user);
        leDs.setPassword(pwd);
        leDs.setUrl(url);
        leDs.setMaxActive(5);
        leDs.setMaxIdle(1);
        leDs.setMinIdle(1);
        leDs.setMaxWait(60 * 1000L);
        logger.info("Create New Db Connection to Laneasy");
    }

    public void initManagerDBPool(String driver, String url, String user, String pwd) {
        managerDs = new BasicDataSource();
        managerDs.setDriverClassName(driver);
        managerDs.setUsername(user);
        managerDs.setPassword(pwd);
        managerDs.setUrl(url);
        managerDs.setMaxActive(5);
        managerDs.setMaxIdle(1);
        managerDs.setMinIdle(1);
        managerDs.setMaxWait(60 * 1000L);
        logger.info("Create New Db Connection to Opmanager ");
    }

    public void initStorDBPool(String driver, String url, String user, String pwd) {
        storDs = new BasicDataSource();
        storDs.setDriverClassName(driver);
        storDs.setUsername(user);
        storDs.setPassword(pwd);
        storDs.setUrl(url);
        storDs.setMaxActive(5);
        storDs.setMaxIdle(1);
        storDs.setMinIdle(1);
        storDs.setMaxWait(60 * 1000L);
        logger.info("Create New Db Connection to Opstor ");
    }

    public void initGabDBPool(String driver, String url, String user, String pwd) {
        gabDs = new BasicDataSource();
        gabDs.setDriverClassName(driver);
        gabDs.setUsername(user);
        gabDs.setPassword(pwd);
        gabDs.setUrl(url);
        gabDs.setMaxActive(5);
        gabDs.setMaxIdle(1);
        gabDs.setMinIdle(1);
        gabDs.setMaxWait(60 * 1000L);
        logger.info("Create New Db Connection to gab ");
    }

    public void initanjianDBPool(String driver, String url, String user, String pwd) {
        anjianDs = new BasicDataSource();
        anjianDs.setDriverClassName(driver);
        anjianDs.setUsername(user);
        anjianDs.setPassword(pwd);
        anjianDs.setUrl(url);
        anjianDs.setMaxActive(5);
        anjianDs.setMaxIdle(1);
        anjianDs.setMinIdle(1);
        anjianDs.setMaxWait(60 * 1000L);
        logger.info("Create New Db Connection to anjianDs ");
    }

    public Connection getLeConnection() {
        Connection conn = null;
        try {
            conn = leDs.getConnection();
            if (conn == null) {
                throw new SQLException("no idle connection");
            }
        } catch (SQLException e) {
            logger.error(e);
        }
        return conn;
    }

    public Connection getManagerConnection() {
        Connection conn = null;
        try {
            conn = managerDs.getConnection();
            if (conn == null) {
                throw new SQLException("no idle connection");
            }
        } catch (SQLException e) {
            logger.error(e);
        }
        return conn;
    }

    public Connection getStorConnection() {
        Connection conn = null;
        try {
            conn = storDs.getConnection();
            if (conn == null) {
                throw new SQLException("no idle connection");
            }
        } catch (SQLException e) {
            logger.error(e);
        }
        return conn;
    }

    public Connection getGabConnection() {
        Connection conn = null;
        try {
            conn = gabDs.getConnection();
            if (conn == null) {
                throw new SQLException("no idle connection");
            }
        } catch (SQLException e) {
            logger.error(e);
        }
        return conn;
    }

    public Connection getanjianConnection() {
        Connection conn = null;
        try {
            conn = anjianDs.getConnection();
            if (conn == null) {
                throw new SQLException("no idle connection");
            }
        } catch (SQLException e) {
            logger.error(e);
        }
        return conn;
    }

    /**
     * dbcp使用委派模型来实现Connection接口
     * 故.close()就成连接归还到连接池 而不会关闭连接
     */
    public void returnConn(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            logger.error("Failed To Return Conn To DS", e);
        }
    }

}
